package com.geeklan.smart.widget.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.geeklan.smart.R;

import java.util.List;

public class CalendarAdapter extends RecyclerView.Adapter<CalendarAdapter.ViewHolder> {

    private List<CalendarDay> dateList;
    private Context context;
    private OnDateClickListener onDateClickListener;
    private int selectedPosition = -1; // 记录选中的位置

    public CalendarAdapter(Context context, List<CalendarDay> dateList, OnDateClickListener onDateClickListener) {
        this.context = context;
        this.dateList = dateList;
        this.onDateClickListener = onDateClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_calendar_day, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CalendarDay date = dateList.get(position);


        if(date.isData()){
            holder.view.setVisibility(View.VISIBLE);
        }else {
            holder.view.setVisibility(View.GONE);
        }

        if (date.getDay() == 0) {
            holder.dateTextView.setText("");
            holder.dateTextView.setBackground(null);
            return;
        }

        holder.dateTextView.setText(date.getDayText());
        // 设置今天的背景
        if (date.isToday()) {
            holder.dateTextView.setBackground(ContextCompat.getDrawable(context, R.drawable.radio_button_today_background));
            holder.dateTextView.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if(isChecked){
                    holder.dateTextView.setTextColor(ContextCompat.getColor(context, R.color.color_9ccc9c));
                }else {
                    holder.dateTextView.setTextColor(ContextCompat.getColor(context, R.color.background_color));
                }
            });
        } else {
            holder.dateTextView.setBackground(ContextCompat.getDrawable(context, R.drawable.radio_button_data_background));
            holder.dateTextView.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if(isChecked){
                    holder.dateTextView.setTextColor(ContextCompat.getColor(context, R.color.color_9ccc9c));
                }else {
                    holder.dateTextView.setTextColor(ContextCompat.getColor(context, R.color.color_333333));
                }
            });
        }

        if (position == selectedPosition) {
            holder.dateTextView.setChecked(true);
        } else {
            holder.dateTextView.setChecked(false);
        }

        // 点击事件处理
        holder.dateTextView.setOnClickListener(v -> {
            int oldPosition = selectedPosition;
            selectedPosition = position;

            notifyItemChanged(oldPosition);
            notifyItemChanged(selectedPosition);

            if (onDateClickListener != null) {
                onDateClickListener.onDateClick(date.getYear(), date.getMonth(), date.getDay());
            }
        });
    }

    @Override
    public int getItemCount() {
        return dateList.size();
    }

    public void updateData(List<CalendarDay> newDays) {
        this.dateList = newDays;
        this.selectedPosition = -1;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        RadioButton dateTextView;
        View view;

        public ViewHolder(View itemView) {
            super(itemView);
            dateTextView = itemView.findViewById(R.id.dateTextView);
            view = itemView.findViewById(R.id.view);
        }
    }

    public interface OnDateClickListener {
        void onDateClick(int year,int month,int day);
    }
}

